﻿using System;
using System.Globalization;

namespace Base.Common.Converters
{
    public class StringNumberConverter : ConverterBase
    {
        /// <summary>
        /// 是否可以为空
        /// </summary>
        public bool IsNullable { get; set; }

        public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value?.ToString();
        }

        public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double d;
            if (double.TryParse(value?.ToString(), out d))
            {
                return d;
            }

            if (IsNullable)
                return null;
            return 0;
        }
    }
}
